package com.tos.second.controller;

import com.tos.second.common.Result.Result;
import com.tos.second.entity.ShoppingCart;
import com.tos.second.service.ShoppingCartService;
import com.tos.second.vo.ShoppingCartGetMyVo;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shoppingCart")

@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加商品到购物车
     * @param goodsId
     * @return
     */
    @PostMapping("/add/{goodsId}")
    public Result<String> addShoppingCart(@PathVariable Long goodsId){
        log.info("添加商品到购物车，商品ID为{}", goodsId);
        shoppingCartService.add(goodsId);
        // 添加商品到购物车
        return Result.success("添加成功");
    }

    @GetMapping("/getMyShoppingCart")
    public Result<List<ShoppingCartGetMyVo>> getShoppingCart(){
        log.info("获取购物车列表");
        // 获取购物车列表
        return Result.success(shoppingCartService.getAll());
    }


    /**
     *  删除购物车商品
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result<String> deleteShoppingCart(@PathVariable Long id){
        log.info("删除购物车商品，购物车ID为{}", id);
        shoppingCartService.delete(id);
        return Result.success("删除成功");
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clear")
    public Result<String> clear(){
        log.info("清空购物车接口");
        shoppingCartService.clear();
        return Result.success("清空成功");
    }

    /**
     * 购买购物车商品
     * @param ids
     * @return
     */
    @PutMapping("/buy")
    public Result<String> buy(@RequestParam String ids){
        log.info("购买购物车商品接口");
        shoppingCartService.buy(ids);
        return Result.success("购买成功");
    }
}
